﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using Castle.ActiveRecord;
using System.Web.UI;
using System.IO;
using SimplePMT.Data;
using SimplePMT.Components;
using NHibernate.Expression;

namespace SimplePMT.Control
{
    public partial class FileControl : System.Web.UI.UserControl
    {
        public bool edit = false;
        public int attachmentid = 0;
        public string attachmentTitle = string.Empty;
        public Attachment attachment;
        public string createDate = string.Empty;
        public string createdBy = string.Empty;
        public string viewTitle = "New File Attachment";
        public string attachmentLocation = string.Empty;
        public int containerId = 0;
        public string updateDate = string.Empty;
        public string taskAssociationList = string.Empty;
        public List<int> intList = new List<int>();


        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        public override string ToString()
        {
            if (Request.QueryString["cid"] != null)
            {
                containerId = Convert.ToInt32(Request.QueryString["cid"]);
            }
            
            using (new SessionScope())
            {
                attachmentLocation = Common.GetRecursiveTaskTreePath(TaskContainer.Find(containerId));
                taskAssociationList = GetTasks(containerId);
                attachment = Attachment.Find(attachmentid) ?? new Attachment();
                if (attachment.ID > 0)
                {
                    attachmentTitle = attachment.Title.Replace("\"", "&quot;");
                    viewTitle = attachment.Title;
                    createDate = attachment.DateTime.Value.ToShortDateString();
                    updateDate = attachment.UpdateDateTime.Value.ToString();
                    TaskContainerAttachment tca = new TaskContainerAttachment();
                    TaskContainerAttachment[] containerItems = TaskContainerAttachment.FindAllByProperty("Attachment", attachment);
                    if (containerItems.Length > 0)
                    {
                        if (containerItems[0].TaskContainer.ParentContainerID > 0)
                        {
                            attachmentLocation = Common.GetRecursiveTaskTreePath(TaskContainer.Find(containerItems[0].TaskContainer.ParentContainerID)) + " : " + containerItems[0].TaskContainer.Label;
                        }
                        else
                        {
                            attachmentLocation = containerItems[0].TaskContainer.Label;
                        }
                    }
                    Member created = Member.Find(attachment.MemberID);
                    createdBy = created.FirstName + " " + created.LastName;
                    
                }
                else
                {
                    attachment.DateTime = new DateTime(1900, 1, 1);
                    createdBy = Session["FullName"].ToString();
                    createDate = DateTime.Now.ToShortDateString();
                }
            }

            string result = "";
            StringWriter writer = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
            this.Render(htmlWriter);
            result = writer.ToString();
            return result;
        }

        public string GetTasks(int tcid)
        {
            StringBuilder ta = new StringBuilder();

            GetRecursiveTaskIDList(TaskContainer.Find(Common.GetRecursiveTaskTreeID(TaskContainer.Find(tcid))));
            Task[] tasks = Task.FindAll(Expression.In("ID", intList), Expression.Eq("ClosedDateTime", Convert.ToDateTime("1/1/1900")));
            string isChecked = " checked";
            foreach (Task task in tasks)
            {
                TaskAttachment[] taa = TaskAttachment.FindAll(Expression.Eq("TaskID", task.ID), Expression.Eq("AttachmentID", attachmentid));
                if (taa.Length > 0)
                {
                    isChecked = " checked"; 
                }
                else
                {
                    isChecked = "";
                }
                ta.Append("<input type=\"checkbox\" OnClick=\"associateTask("+ task.ID +", "+ attachmentid +", this);\""+ isChecked +">&nbsp;&nbsp;"+ task.Title +"<br>");
            }
            return ta.ToString();
        }

        private void GetRecursiveTaskIDList(TaskContainer parent)
        {
            TaskContainer[] containers = TaskContainer.FindAll(Expression.Eq("ParentContainerID", parent.ID));
            foreach (TaskContainer container in containers)
            {
                TaskContainerItem[] items = TaskContainerItem.FindAll(Expression.Eq("TaskContainer", container));
                foreach (TaskContainerItem item in items)
                {
                    intList.Add(item.TaskID);
                }
                GetRecursiveTaskIDList(container);
            }
        }
    }
}